package com.qbw.app.manager;

import com.qbw.actionmanager.ActionManager;
import com.qbw.app.Constant;
import com.qbw.app.action.UnitChangedAction;
import com.qbw.spm.P;

/**
 * @author QBW
 * @date 2019/10/14
 */
public class UnitManager {

    private static UnitManager sInstance;

    private boolean mUseKm;

    private UnitManager() {
        mUseKm = isKm();
    }

    public static UnitManager getInstance() {
        if (sInstance == null) {
            synchronized (UnitManager.class) {
                if (sInstance == null) {
                    sInstance = new UnitManager();
                }
            }
        }
        return sInstance;
    }

    public void setUnit(boolean isKm) {
        mUseKm = isKm;
        P.putString(Constant.KEY_UNIT, isKm ? Constant.UNIT_KPH : Constant.UNIT_MPH);
        ActionManager.getInstance().triggerAction(new UnitChangedAction());
    }

    private boolean isKm() {
        return Constant.UNIT_KPH.equals(P.getString(Constant.KEY_UNIT, Constant.UNIT_DEFAULT));
    }

    public boolean isUseKm() {
        return mUseKm;
    }
}
